# Maintainer: Peter Budai <peterbudai@hotmail.com>

_realname=tornado
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=6.0.3
pkgrel=2
pkgdesc="open source version of the scalable, non-blocking web server and tools (mingw-w64)"
arch=('any')
url='https://www.tornadoweb.org/'
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-futures"
             "${MINGW_PACKAGE_PREFIX}-python2-backports-abc"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-singledispatch"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/tornadoweb/tornado/archive/v${pkgver}.tar.gz")
sha256sums=('a97ac3b8c95867e534b48cb6fbbf156f5ca5b20c423bb06894c17b240d7a18fc')

noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  # work around an issue with symbolic links in the tarball.
  bsdtar zxf ${_realname}-${pkgver}.tar.gz || true

  for pver in {2,3}; do
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done

  # python -> python2 rename
  find python2-build-${CARCH} -name '*py' -exec sed -e 's_#!/usr/bin/env python_&2_' -i {} \;

  export TORNADO_EXTENSION=1
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

# We probably will need various prerequisits for this
#check() {
#  for pver in {2,3}; do
#    msg "Python ${pver} test for ${CARCH}"
#    cd "${srcdir}/python${pver}-build-${CARCH}"
#    ${MINGW_PREFIX}/bin/python${pver} setup.py check
#  done
#}

package_python3-tornado() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
}

package_python2-tornado() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-futures"
           "${MINGW_PACKAGE_PREFIX}-python2-backports-abc"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python2-singledispatch")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
}

package_mingw-w64-i686-python2-tornado() {
  package_python2-tornado
}

package_mingw-w64-i686-python3-tornado() {
  package_python3-tornado
}

package_mingw-w64-x86_64-python2-tornado() {
  package_python2-tornado
}

package_mingw-w64-x86_64-python3-tornado() {
  package_python3-tornado
}
